package com.hbean.service.impl;

import com.hbean.entity.SysUserRole;
import com.hbean.mapper.SysUserRoleMapper;
import com.hbean.service.SysUserRoleService;
import com.hbean.utils.Condition;
import com.hbean.utils.HUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-10-28 00:02
 * @ModificationHistory:
 */

@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    @CachePut(value = "sysCache", key="'sysUserRole'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysUserRole> insertDataAndUpdateCache(SysUserRole sysUserRole) {
        String su00 = HUtil.jsonStrToKeyValue(sysUserRole.getTemp1(),"su00");
        String roleIds = HUtil.jsonStrToKeyValue(sysUserRole.getTemp1(),"temp1");
        List<String> list = HUtil.stringToList(roleIds,",");

        List<SysUserRole> sysUserRoles = new ArrayList<>();
        list.forEach(s -> {
            SysUserRole sysUserRole1 = new SysUserRole();
            sysUserRole1.setSur00(HUtil.uuidOf18());
            sysUserRole1.setSur01(su00);
            sysUserRole1.setSur02(s);
            sysUserRoles.add(sysUserRole1);
        });

        SysUserRole sysUserRole1 = new SysUserRole();
        sysUserRole1.setSur01(su00);
        this.sysUserRoleMapper.deleteData(sysUserRole1);

        this.sysUserRoleMapper.insertDatas(sysUserRoles);

        return this.sysUserRoleMapper.selectDatas(new Condition());
    }

    @Override
    public List<SysUserRole> deleteDataAndUpdateCache(SysUserRole sysUserRole) {
        return null;
    }

    @Override
    public List<SysUserRole> updateDataAndUpdateCache(SysUserRole sysUserRole) {
        return null;
    }

    @Override
    @Cacheable(value = "sysCache", key="'sysUserRole'") //开启缓存策略
    public List<SysUserRole> getDataForCache() {
        return this.sysUserRoleMapper.selectDatas(new Condition());
    }

}
